<template>
	<view class="notice-list-container" :style="{ paddingTop: (statusBarHeight + 88) + 'rpx' }">
		<!-- 头部 -->
		<view class="header" :style="{ paddingTop: statusBarHeight + 'rpx', height: (statusBarHeight + 88) + 'rpx' }">
			<view class="header-left" @click="goBack">
				<text class="back-icon">‹</text>
			</view>
			<text class="header-title">{{ pageTitle }}</text>
			<view class="header-right"></view>
		</view>

		<!-- 通知列表 -->
		<view class="notice-list">
			<view 
				v-for="(item, index) in noticeList" 
				:key="index"
				@click="goToNoticeDetail(item)"
				class="notice-item"
			>
				<view class="notice-content">
					<view class="notice-header">
						<text class="notice-title">{{ item.title || '通知标题' }}</text>
						<text class="notice-time">{{ formatTime(item.create_time || item.createTime) }}</text>
					</view>
					<text class="notice-desc">{{ item.content || item.description || '通知内容' }}</text>
					<view v-if="!item.is_read" class="unread-dot"></view>
				</view>
				<text class="arrow-icon">›</text>
			</view>
		</view>

		<!-- 空状态 -->
		<view v-if="noticeList.length === 0 && !loading" class="empty-state">
			<image src="/static/icons/empty-state.svg" class="empty-icon"></image>
			<text class="empty-text">暂无通知</text>
		</view>

		<!-- 加载状态 -->
		<view v-if="loading" class="loading-state">
			<text class="loading-text">加载中...</text>
		</view>
	</view>
</template>

<script>
import { systemApi } from '@/api/index.js'

export default {
	data() {
		return {
			type: '', // 通知类型
			pageTitle: '通知列表',
			noticeList: [],
			loading: false,
			page: 1,
			receiver: '1,2,3,4,5', // 接收者类型
			statusBarHeight: 0 // 状态栏高度
		}
	},
	
	onLoad(options) {
		// 设置状态栏高度
		const systemInfo = uni.getSystemInfoSync()
		const statusBarHeight = systemInfo.statusBarHeight || 0
		// 在小程序环境中，直接使用rpx单位
		this.statusBarHeight = statusBarHeight * 2 // 转换为rpx
		
		if (options.type) {
			this.type = options.type
			this.setPageTitle()
		}
		this.loadNoticeList()
	},
	
	methods: {
		// 设置页面标题
		setPageTitle() {
			const titleMap = {
				'1': '返现通知',
				'2': '物流通知', 
				'3': '申请通知',
				'4': '互动通知',
				'5': '系统通知'
			}
			this.pageTitle = titleMap[this.type] || '通知列表'
		},
		
		// 加载通知列表
		async loadNoticeList() {
			if (this.loading) return
			
			this.loading = true
			try {
				const params = {
					type: this.type,
					page: this.page,
					receiver: this.receiver
				}
				
				const res = await systemApi.getNoticeDetailList(params)
				
				if (res.r === 0) {
					if (res.data && res.data.lists) {
						if (this.page === 1) {
							this.noticeList = res.data.lists
						} else {
							this.noticeList = [...this.noticeList, ...res.data.lists]
						}
					} else {
						this.noticeList = []
					}
				} else {
					uni.showToast({
						title: res.msg || '获取通知列表失败',
						icon: 'none'
					})
				}
			} catch (err) {
				console.error('获取通知列表失败:', err)
				uni.showToast({
					title: '网络错误，请重试',
					icon: 'none'
				})
			} finally {
				this.loading = false
			}
		},
		
		// 格式化时间
		formatTime(timeStr) {
			if (!timeStr) return ''
			
			const date = new Date(timeStr)
			const now = new Date()
			const diff = now.getTime() - date.getTime()
			
			// 一天内显示时分
			if (diff < 24 * 60 * 60 * 1000) {
				const hour = String(date.getHours()).padStart(2, '0')
				const minute = String(date.getMinutes()).padStart(2, '0')
				return `${hour}:${minute}`
			}
			
			// 超过一天显示月日
			const month = String(date.getMonth() + 1).padStart(2, '0')
			const day = String(date.getDate()).padStart(2, '0')
			return `${month}-${day}`
		},
		
		// 跳转到通知详情
		goToNoticeDetail(item) {
			uni.navigateTo({
				url: `/pages/news/detail?id=${item.id || item.notice_id}&type=${this.type}`
			})
		},
		
		// 返回上一页
		goBack() {
			uni.navigateBack()
		},
		
		// 下拉刷新
		onPullDownRefresh() {
			this.page = 1
			this.loadNoticeList().then(() => {
				uni.stopPullDownRefresh()
			})
		},
		
		// 上拉加载更多
		onReachBottom() {
			this.page++
			this.loadNoticeList()
		}
	}
}
</script>

<style scoped>
.notice-list-container {
	background-color: #f5f5f5;
	min-height: 100vh;
}

/* 头部样式 */
.header {
	display: flex;
	align-items: flex-end;
	justify-content: space-between;
	padding: 0 30rpx 20rpx 30rpx;
	background: linear-gradient(135deg, #FF6B6B, #FF8E8E);
	color: white;
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	z-index: 999;
	box-sizing: border-box;
}

.header-left {
	width: 60rpx;
	height: 60rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.back-icon {
	font-size: 48rpx;
	color: white;
	font-weight: bold;
}

.header-title {
	font-size: 36rpx;
	font-weight: bold;
	color: white;
}

.header-right {
	width: 60rpx;
}

/* 通知列表样式 */
.notice-list {
	padding: 20rpx 0;
}

.notice-item {
	display: flex;
	align-items: center;
	padding: 30rpx;
	background-color: #fff;
	margin-bottom: 2rpx;
	position: relative;
}

.notice-item:active {
	background-color: #f8f8f8;
}

.notice-content {
	flex: 1;
	min-width: 0;
	position: relative;
}

.notice-header {
	display: flex;
	justify-content: space-between;
	align-items: flex-start;
	margin-bottom: 12rpx;
}

.notice-title {
	font-size: 32rpx;
	color: #333;
	font-weight: 500;
	flex: 1;
	margin-right: 20rpx;
}

.notice-time {
	font-size: 24rpx;
	color: #999;
	flex-shrink: 0;
}

.notice-desc {
	font-size: 28rpx;
	color: #666;
	line-height: 1.4;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 2;
	overflow: hidden;
}

.unread-dot {
	position: absolute;
	top: 0;
	right: -10rpx;
	width: 16rpx;
	height: 16rpx;
	background-color: #FF6B6B;
	border-radius: 50%;
}

.arrow-icon {
	font-size: 32rpx;
	color: #ccc;
	font-weight: bold;
	margin-left: 20rpx;
}

/* 空状态样式 */
.empty-state {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding: 120rpx 40rpx;
}

.empty-icon {
	width: 120rpx;
	height: 120rpx;
	margin-bottom: 30rpx;
	opacity: 0.6;
}

.empty-text {
	font-size: 28rpx;
	color: #999;
}

/* 加载状态样式 */
.loading-state {
	display: flex;
	justify-content: center;
	align-items: center;
	padding: 60rpx;
}

.loading-text {
	font-size: 28rpx;
	color: #999;
}
</style>